package com.wealth.scsj.b_app.view.activity.shopregistermodel;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.vondear.rxtools.RxPhotoTool;
import com.vondear.rxtools.RxSPTool;
import com.vondear.rxtools.view.dialog.RxDialogChooseImage;
import com.wealth.scsj.b_app.R;
import com.wealth.scsj.b_app.bean.ShoperCommitInfoBean;
import com.wealth.scsj.b_app.utils.GetFileByUri;
import com.wealth.scsj.b_app.utils.SkipUtils;
import com.wealth.scsj.b_app.view.activity.BaseActivity;
import com.yalantis.ucrop.UCrop;
import com.yalantis.ucrop.UCropActivity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import es.dmoral.toasty.Toasty;

import static com.vondear.rxtools.view.dialog.RxDialogChooseImage.LayoutType.TITLE;

/**
 * 商家申请入驻第四步
 */
public class RegisterThirdStepActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout nextBtnRl;
    private ShoperCommitInfoBean shoperCommitInfoBean;
    private ImageView imgInfoIv;
    private Uri resultUri;
    private EditText lisencenameEt;
    private EditText lisencenameNumEt;
    private EditText origanNumEt;
    private EditText bossNameEt;
    private EditText idCardNumEt;
    private EditText bossTelEt;

    private LinearLayout loadImgLl;
    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register_third_step);
    }

    @Override
    protected void initView() {
        nextBtnRl = findViewById(R.id.registerthird_next_rl);
        loadImgLl = findViewById(R.id.step_three_load_img);
        imgInfoIv = findViewById(R.id.step_three_iv);
        lisencenameEt = findViewById(R.id.registerthird_lisencename_et);
        lisencenameNumEt = findViewById(R.id.registerthird_lisencenum_et);
        origanNumEt = findViewById(R.id.registerthird_origan_num);
        bossNameEt = findViewById(R.id.registerthird_ownername_et);
        idCardNumEt = findViewById(R.id.registerthird_idnum_et);
        bossTelEt = findViewById(R.id.registerthird_ownertel_et);
    }

    @Override
    protected void initListener() {
        nextBtnRl.setOnClickListener(this);
        loadImgLl.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        shoperCommitInfoBean = (ShoperCommitInfoBean) getIntent().getSerializableExtra("delivery_shopdata2");
        //获取第一个页面传递过来的数据
        //店铺名字
        String shopName = shoperCommitInfoBean.getShopName();
        //先打印一下看看
        Log.e("传过来的店铺类型333：", shoperCommitInfoBean.getShopType() + "类型");
        Log.e("传过来的标准距离333：", shoperCommitInfoBean.getStandardDistance() + "米");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.registerthird_next_rl:
                SkipUtils.toRegisterFourStep(this, shoperCommitInfoBean);
                //执照名称
                String lisencename = lisencenameEt.getText().toString();
                //执照注册号
                String lisencenameNum = lisencenameNumEt.getText().toString();
                //组织机构代码
                String origanNum = origanNumEt.getText().toString();
                //法人名称
                String bossName = bossNameEt.getText().toString();
                //法人身份证号
                String idCardNum = idCardNumEt.getText().toString();
                //法人电话
                String bossTel = bossTelEt.getText().toString();

                if (TextUtils.isEmpty(lisencename)) {
                    Toasty.info(RegisterThirdStepActivity.this, "请输入营业执照").show();
                    return;
                } else if (TextUtils.isEmpty(lisencenameNum)) {
                    Toasty.info(RegisterThirdStepActivity.this, "请输入营业执照注册号").show();
                } else if (TextUtils.isEmpty(origanNum)) {
                    Toasty.info(RegisterThirdStepActivity.this, "请输入组织机构代码").show();
                } else if (TextUtils.isEmpty(bossName)) {
                    Toasty.info(RegisterThirdStepActivity.this, "请输入法人名称").show();
                } else if (TextUtils.isEmpty(idCardNum)) {
                    Toasty.info(RegisterThirdStepActivity.this, "请输入法人身份证号").show();
                } else if (TextUtils.isEmpty(bossTel)) {
                    Toasty.info(RegisterThirdStepActivity.this, "请输入法人电话").show();
                } else {
                    shoperCommitInfoBean.setLisencename(lisencename);
                    shoperCommitInfoBean.setLisencenameNum(lisencenameNum);
                    shoperCommitInfoBean.setOriganNum(origanNum);
                    shoperCommitInfoBean.setBossName(bossName);
                    shoperCommitInfoBean.setIdCardNum(idCardNum);
                    shoperCommitInfoBean.setBossTel(bossTel);

                    SkipUtils.toRegisterFourStep(this, shoperCommitInfoBean);
                }

                break;

                case R.id.step_three_load_img:
                    //上传资质图片
                    initDialogChooseImage();
                    break;

        }

    }

    private void initDialogChooseImage() {
        RxDialogChooseImage dialogChooseImage = new RxDialogChooseImage(this, TITLE);
        dialogChooseImage.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        //上传图片的同时将图片控件显示出来
        imgInfoIv.setVisibility(View.VISIBLE);
        switch (requestCode) {
            case RxPhotoTool.GET_IMAGE_FROM_PHONE://选择相册之后的处理
                if (resultCode == RESULT_OK) {
                    initUCrop(data.getData());
                }
                break;
            case RxPhotoTool.GET_IMAGE_BY_CAMERA://选择照相机之后的处理
                if (resultCode == RESULT_OK) {
                    initUCrop(RxPhotoTool.imageUriFromCamera);
                }
                break;
            case RxPhotoTool.CROP_IMAGE://普通裁剪后的处理
                Glide.with(this).
                        load(RxPhotoTool.cropImageUri).
                        diskCacheStrategy(DiskCacheStrategy.RESULT).
                        thumbnail(0.5f).
                        placeholder(R.drawable.ic_launcher_background).
                        priority(Priority.HIGH).
                        error(R.drawable.ic_launcher_background).
                        fallback(R.drawable.ic_launcher_background).
                        into(imgInfoIv);
                //开始上传图片
                //获取图片真实路径
//                String path = GetFileByUri.getFileByUri(resultUri, getApplicationContext()).getPath();
//                ImageLoder imageLoder = new ImageLoder();
//                imageLoder.startLoad(path, shoperCommitInfoBean, 3);
                break;
            case UCrop.REQUEST_CROP://UCrop裁剪之后的处理
                if (resultCode == RESULT_OK) {
                    resultUri = UCrop.getOutput(data);
                    roadImageView(resultUri, imgInfoIv);
                    RxSPTool.putContent(this, "AVATAR", resultUri.toString());
                } else if (resultCode == UCrop.RESULT_ERROR) {
                    final Throwable cropError = UCrop.getError(data);
                }
                break;
            case UCrop.RESULT_ERROR://UCrop裁剪错误之后的处理
                final Throwable cropError = UCrop.getError(data);
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void initUCrop(Uri uri) {
        SimpleDateFormat timeFormatter = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA);
        long time = System.currentTimeMillis();
        String imageName = timeFormatter.format(new Date(time));

        Uri destinationUri = Uri.fromFile(new File(getCacheDir(), imageName + ".jpeg"));

        UCrop.Options options = new UCrop.Options();
        //设置裁剪图片可操作的手势
        options.setAllowedGestures(UCropActivity.SCALE, UCropActivity.ROTATE, UCropActivity.ALL);
        //设置隐藏底部容器，默认显示
        //options.setHideBottomControls(true);
        //设置toolbar颜色
        options.setToolbarColor(ActivityCompat.getColor(this, R.color.colorPrimary));
        //设置状态栏颜色
        options.setStatusBarColor(ActivityCompat.getColor(this, R.color.colorPrimaryDark));
        //开始设置
        //设置最大缩放比例
        options.setMaxScaleMultiplier(5);
        //设置图片在切换比例时的动画
        options.setImageToCropBoundsAnimDuration(666);
        //设置裁剪窗口是否为椭圆
        //options.setOvalDimmedLayer(true);
        //设置是否展示矩形裁剪框
        // options.setShowCropFrame(false);
        //设置裁剪框横竖线的宽度
        //options.setCropGridStrokeWidth(20);
        //设置裁剪框横竖线的颜色
        //options.setCropGridColor(Color.GREEN);
        //设置竖线的数量
        //options.setCropGridColumnCount(2);
        //设置横线的数量
        //options.setCropGridRowCount(1);
        UCrop.of(uri, destinationUri)
                .withAspectRatio(1, 1)
                .withOptions(options)
                .start(this);
    }

    //从Uri中加载图片 并将其转化成File文件返回
    private File roadImageView(Uri uri, ImageView imageView) {
        Glide.with(this).
                load(uri).
                diskCacheStrategy(DiskCacheStrategy.RESULT).
                placeholder(R.drawable.ic_launcher_background).
                priority(Priority.LOW).
                error(R.drawable.ic_launcher_background).
                fallback(R.drawable.ic_launcher_background).
                into(imgInfoIv);

//        //开始上传图片
//        ImageLoder imageLoder = new ImageLoder();
//        //获取图片真实路径
//        String path = GetFileByUri.getFileByUri(resultUri, getApplicationContext()).getPath();
//        imageLoder.startLoad(path, shoperCommitInfoBean, 3);

        return (new File(RxPhotoTool.getImageAbsolutePath(this, uri)));
    }
}
